<?php
namespace Web\Logic;
class MenuGetLogic {



    /**
     * 查询子栏目数据
     * @param unknown $id
     * * @param 权限栏目 $roleid
     * @return Ambigous <mixed, boolean, NULL, string, unknown, multitype:, object>
     */
    function selZmenuSql($id,$roleid){
    	try {
    		//实例化模块
    		$mod = M("menu");
    		//查询条件
    		$whers = "parentid = '$id'";
    		if($roleid){
    			$whers .= " and menuid in ($roleid) ";
    		}
    		//查询信息
    		$listRows = $mod->where($whers)
    		->field("menuid,menuname,url,parentid,sorts,oico
    			    ,case when sorts is null then 99
                    when sorts >0 then sorts end as sorte")
            ->order('sorts ASC')
    		->select();
    		return $listRows;
    	} catch (Exception $e) {
    	}
    }





    /**
     * 栏目处理
     * @param $menu_list 用户拥有的栏目权限
     * $current_id当前打开的栏目id
     */

    function dealWithMenu($menu_list,$current_id){
    	//返回值
    	$redata = "";
    	//获取导航
    	$dhMenu = $this->getDhl(0, $menu_list);
    	//获取子栏目
    	for($i=0; $i<count($dhMenu);$i++){
    		$redata .= $this->selZiMenu($dhMenu[$i]['menuid'],$menu_list,$current_id);
    	}
    	$return[0] =$dhMenu;
        $return[1] =$redata;
    	//返回值整理
    	return $return;
    }

     /**
     * @desc 查询导航栏
     * @param $id 上级id
     * @param $roleid  栏目权限
     */
    function getDhl($id,$menu_list)
    {
        //实例化模块
        $mod = M("menu");
        if($menu_list){
            $where = " and menuid in ($menu_list) ";
        }
        //获取父栏目id
        if($id >0)
            $wdata = "parentid = $id".$where;
        else
            $wdata = "(parentid = 0 or parentid is null)".$where;
        $listrow =  $mod
        ->where($wdata)
        ->field("menuid,menuname,url,parentid,sorts
                    ,case when sorts is null then 99
                    when sorts >0 then sorts end as sorte")
        ->order('sorte asc')
        ->select();
        //return $wdata;
        return $listrow;
    }


     /**
     *
     * @param unknown $id
     * @param 栏目权限 $roleid
     * @return multitype:multitype:NULL string multitype:multitype:NULL string
     */
     function selZiMenu($id,$roleid,$current_id){
        //返回数据
        $redata = "";
        //获取栏目
        $menuData = $this->selZmenuSql($id,$roleid);
        //循环数据
        for($n=0;$n<count($menuData);$n++){
            //跳转地址
            $url = "";
            //判断
            if($menuData[$n]["url"]){
                $url = U($menuData[$n]["url"],array('urlid'=>$menuData[$n]["menuid"]));
            }
            $oico = $menuData[$n]["oico"]?$menuData[$n]["oico"]:"fa-caret-right";

            if($menuData[$n]["url"] )
            {
                $class ="";
                if(in_array($menuData[$n]['menuid'], $current_id)){
                    $class ="active";
                }
                $redata .='<li class="'.$class.'">';

                $redata .= '<a data-url ="'.$url.'" href="'.$url.'"  >
                                <i class="menu-icon fa '.$oico.'"></i>
                                <span>'.$menuData[$n]["menuname"].'</span>
                            </a>';
             }else{
                    $class ="";
                    if(in_array($menuData[$n]['menuid'], $current_id)){
                        $class ="active";
                    }
                    if($menuData[$n]['parentid'] > 1){
                        $menu = '<span class="menu-arrow left-has-menu"><i class="mdi mdi-chevron-right"></i></span>';
                        
                    }else{
                        $menu = '<span class="menu-arrow"><i class="mdi mdi-chevron-right"></i></span>'; 
                    }
                   $redata .='<li class="'.$class.'">';
                   $redata .= '<a  class="'.$class.'">
                                    <i class="menu-icon fa '.$oico.'"></i>
                                    <span>'.$menuData[$n]["menuname"].'</span>
                                    '.$menu.'
                                </a>';
             }

            //查询是否有子栏目
            $num = $this->getChdCount($menuData[$n]['menuid']);
            if($num>0){
                 $class ="";
                 if(in_array($menuData[$n]['menuid'],$current_id)){
                        $class ="in";
                 }
                $redata .= '<ul class="nav-second-level collapse '.$class.'" >';
                $redata .= $this->selZiMenu($menuData[$n]['menuid'],$roleid,$current_id);
                $redata .= '</ul>';
            }
            $redata .='</li>';
        }
        return $redata;
    }
      /**
     * @desc 查询子栏目个数
     *
     */
    private function getChdCount($id)
    {
        //实例化模块
        $mod = M("menu");
        $listrow =  $mod
        ->where("parentid = '$id'")
        ->field("menuid")
        ->count();
        return $listrow;
    }



    function dealWithMenuForRoleSet($set_role_menu_id){
        //返回值
        $redata = array();
        //获取导航
        $dhMenu = $this->getDhl(0,$set_role_menu_id);


        $zMenu = array();
        //获取子栏目s
        for($i=0; $i<count($dhMenu);$i++){
            $zh['id'] = $dhMenu[$i]['menuid'];
            $zh['text'] = $dhMenu[$i]['menuname'];
            $zh['items'] = $this->selZiMenus($dhMenu[$i]['menuid'],$set_role_menu_id);
            $zMenu[$i] = $zh;
        }

        //返回值整理
        $redata = array($dhMenu,$zMenu);
        return $redata;
    }
   function selZiMenus($id,$roleid){
    //返回数据
    $redata = array();
    //获取栏目
    $menuData = $this->selZmenuSql($id,$roleid);

    //循环数据
    for($n=0;$n<count($menuData);$n++){
        $d= array();
        $num = 0;
        $d['text'] = $menuData[$n]['menuname'];
        $d['id'] = $menuData[$n]['menuid'];
        //查询是否有子栏目
        $num = $this->getChdCount($menuData[$n]['menuid']);
        if($num>0){
            $d['items'] = $this->selZiMenus($menuData[$n]['menuid'],$roleid);
        }else{
            $d['items'] =array();
        }
        $redata[] = $d;
    }
    return $redata;
    }


}